<?php
/**
 * 
 * 任务 处理类
 * @author tanzhe
 *
 */
class TaskModel {
	private $db;
	private $cache;
	private $shopRequire;
	public function __construct() {
		$this->db = new Data();
		$this->cache = new Cache();
		global $shopRequire;
		$this->shopRequire = $shopRequire;
	}
	
	/**
	 * 
	 * 获得任务信息
	 * @param str $key
	 * @param int $cache 0 不判断缓存  1、从缓存取
	 */
	public function getTask($key, $cache=1) { 
	 	$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		if ( empty($playerInfo['task']) ) {
			return array('code'=>0,'ret'=>'');
		}
		$taskRecord = $cache == 1 ? $this->cache->get('task_record_'.$playerInfo['u_id']) : array();
		if ( empty($taskRecord) ) {
			$query = $this->db->query("select `status` from player_task where `u_id`={$playerInfo['u_id']} and `label`='{$playerInfo['task']}'");
			$data = $this->db->fetchOneByQuery($query);
			$taskRecord = array(
				$playerInfo['task']=>$data['status'],
			);
			$this->cache->set('task_record_'.$playerInfo['u_id'], $taskRecord);
		}
		return array('code'=>0,'ret'=>array('task_status'=>$taskRecord));
	}

	/**
	 * 
	 * 判断任务是否完成
	 * @param str $key
	 */
	function isFinish($key) {
		$playerInfo = $this->cache->get('ku_'.$key);
		if ( empty($playerInfo['task']) ) { //没有任务
			return false;
		}
		$taskRecord = $this->cache->get('task_record_'.$playerInfo['u_id']);
		if ( empty($taskRecord) ) {
			return false;
		}
		if ( $taskRecord[$playerInfo['task']] != 0 ) {
			return false;
		}
		//任务数据
		$taskInfo = $this->cache->get('task_info_'.$playerInfo['task']);
		if ( empty($taskInfo) ) {
			$query = $this->db->query("select * from task where `label`='{$playerInfo['task']}'");
			$taskInfo = $this->db->fetchOneByQuery($query);
			$this->cache->set('task_info_'.$playerInfo['task'], $taskInfo);
		}
		$require = unserialize($taskInfo['require']);
		foreach ( $require as $k=>$v ) {
			switch ($k) {
				case 'coin': //游戏币
					if ( $v > $playerInfo['coin'] ) {
						return false;
					}
				break;
				case 'cash': //元宝
					if ( $v > $playerInfo['cash'] ) {
						return false;
					}
				break;
			}
		}
		//更新数据库与缓存
		$updateData = array();
		$updateData['status'] = 1;
		//更新角色信息
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
				'label'=>$playerInfo['task'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_task', $options);
		$this->cache->set('task_record_'.$playerInfo['u_id'], array($playerInfo['task']=>1));
		return $playerInfo['task'];
	}
	
	/**
	 * 
	 * 获取任务奖励
	 * @param str $key
	 */
	function getBonus($key) {
		$playerInfo = $this->cache->get('ku_'.$key);
		$chkRet = Common::checkPlayerInfo($playerInfo);
		if($chkRet != 0) {
			return array('code'=>$chkRet,'ret'=>'');
		}
		$taskLabel = $playerInfo['task'];
		if ( empty($taskLabel) ) {
			return array('code'=>231,'ret'=>'');
		}
		$taskRecord = $this->cache->get('task_record_'.$playerInfo['u_id']);
		if ( empty($taskRecord) ) {
			return array('code'=>231,'ret'=>'');
		}
		if ( $taskRecord[$taskLabel] == 0 ) {
			return array('code'=>232,'ret'=>'');
		}
		if ( $taskRecord[$taskLabel] == 2 ) {
			return array('code'=>233,'ret'=>'');
		}
		//任务数据
		$taskInfo = $this->cache->get('task_info_'.$taskLabel);
		if ( empty($taskInfo) ) {
			$query = $this->db->query("select * from task where `label`='{$taskLabel}'");
			$taskInfo = $this->db->fetchOneByQuery($query);
			$this->cache->set('task_info_'.$taskLabel, $taskInfo);
		}
		//更新数据库与缓存
		$updateData = array();
		if ( !empty($taskInfo['coin']) ) {
			$updateData['coin'] = $playerInfo['coin'] + $taskInfo['coin'];
		}
		if ( !empty($taskInfo['cash']) ) {
			$updateData['cash'] = $playerInfo['cash'] + $taskInfo['cash'];
		}
		//更新角色信息
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_info', $options);
		$playerInfo = array_merge($playerInfo, $updateData);
		$this->cache->set("ku_".$key, $playerInfo);
		//写元宝log
		if ( $taskInfo['cash'] > 0 ) {
			$aryData = array(
				'label'=>$taskLabel,
			);
			$options = array(
				'u_id'=> $playerInfo['u_id'],
				'type'=> 'task',
				'num'=> $taskInfo['cash'],
				'data'=> serialize($aryData),
				'add_time'=> time(),
			);
			Common::cashLog($options);
		}
		//更新任务信息
		$updateData = array();
		$updateData['status'] = 2;
		$updateData['bonus_time'] = time();
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
				'label'=>$taskLabel,
			),
			'data'=>$updateData,
		);
		$this->db->updateData('player_task', $options);
		$this->cache->set('task_record_'.$playerInfo['u_id'], array($taskLabel=>2));
		//判断有没有新任务
		$followUp = empty($taskInfo['follow_up']) ? '' : $taskInfo['follow_up'];
		if ( !empty($taskInfo['follow_up']) ) {
			$this->addTask($taskInfo['follow_up'], $playerInfo);
		} else {
			$this->cache->delete("task_record_".$playerInfo['u_id']);
		}
		//返回个人信息
		$ret = array(
			'coin'=>$playerInfo['coin'],
			'cash'=>$playerInfo['cash'],
			'follow_up'=>$followUp,
		);
		return array('code'=>0,'ret'=>$ret);
	}
	
	function addTask($taskLabel, &$playerInfo) { //添加任务
		//更新角色信息
		$updateData = array(
			'task'=>$taskLabel,
		);
		$options = array(
			'key'=>array(
				'u_id'=>$playerInfo['u_id'],
			),
			'data'=>$updateData,
		);
		$playerInfo['task'] = $taskLabel;
		$this->db->updateData('player_info', $options);
		$this->cache->set("ku_".$playerInfo['acc_key'], $playerInfo);
		//插入任务表
		$insertData = array(
			'u_id'=>$playerInfo['u_id'],
			'label'=>$taskLabel,
			'start_time'=>time(),
			'status'=>0,
			'bonus_time'=>0,
		);
		$this->db->insertData('player_task', $insertData);
		$this->cache->set('task_record_'.$playerInfo['u_id'], array($taskLabel=>0));
		return true;
	}
}
?>